import Vue from "vue";
export default {
  //加商品个数
  addCount(state, payload) {
    payload.count++;
  },
  //商品加入购物车
  addToCat(state, payload) {
    payload.checked = true;
    state.cartList.push(payload);
  },
  //改变选中
  changeChecked(state, payload) {
    for (let i = 0; i < state.cartList.length; i++) {
      if (state.cartList[i].iid === payload) {
        // state.cartList[i].checked = !state.cartList[i].checked;
        Vue.set(state.cartList[i], "checked", !state.cartList[i].checked);
        return;
      }
    }
  },
  //全选
  selectAll(state) {
    state.cartList.forEach((value) => {
      value.checked = true;
    });
  },
  //全不选
  selectNone(state) {
    state.cartList.forEach((value) => {
      value.checked = false;
    });
  },
  //删除所选商品
  deleteSelect(state) {
    let newArr = state.cartList.filter((value) => {
      return value.checked === false;
    });
    state.cartList = newArr;
  },
  //修改登录状态
  loginState(state) {
    state.isLogin = !state.isLogin;
  },
  //add浏览记录
  addBrowsingHistoryList(state, payload) {
    let index = null;
    for (let i = 0; i < state.browsingHistoryList.length; i++) {
      if (state.browsingHistoryList[i].iid === payload.iid) {
        index = i;
      }
    }
    if (index !== null) {
      state.browsingHistoryList.splice(index, 1);
    }
    state.browsingHistoryList.unshift(payload);
  },
  updateBuyList(state, payload) {
    state.buyList = [];
    state.buyList.push(...payload);
  },
};
